app.controller('BannerOneCtrl', ['$scope', '$timeout', '$http', '$compile', 'toaster', '$location', '$modal', 'R', 'FileUploader', '$q', '$rootScope',
function($scope, $timeout, $http, $compile, toaster, $location, $modal, R, FileUploader, $q, $rootScope) {

	var urlparams = $location.search();
    $scope.type = urlparams.type;

    if ($scope.type) {
    	$scope.item = $rootScope.getSgObj('BANNERONE');
    	
	    R.getAllBannerByType({type: $scope.type}).then(function(data){
            $scope.pics = data.bizData;
        	
        	$scope.item.nums = $scope.pics.length;

        	angular.forEach($scope.pics, function(item){
        		item.uploader = new FileUploader({
			        url: R.getFileUpload(),
			        formData: [{
			            type: 0
			        }],
			        autoUpload: true,
			        alias: 'inputFile'
			    });

			    item.uploader.onCompleteItem = function(res, data){
			      	item.picUrl = data.bizData.attachmentUrl;
			    };
        	});
        });

    	if (!$scope.item){
            $location.url("/app/banner/list");
    	}
    }

    $scope.mappers = {
        state: [{
            'id': 0,
            'name': '不启用'
        },{
            'id': 1,
            'name': '启用'
        }],
        nums: [{
            'id': 0,
            'name': 0
        },{
            'id': 1,
            'name': 1
        },{
            'id': 2,
            'name': 2
        },{
            'id': 3,
            'name': 3
        },{
            'id': 4,
            'name': 4
        },{
            'id': 5,
            'name': 5
        },{
            'id': 6,
            'name': 6
        },{
            'id': 7,
            'name': 7
        },{
            'id': 8,
            'name': 8
        },{
            'id': 9,
            'name': 9
        }]
    }

    $scope.$watch("item.nums", function(newval){
    	if (newval < 0) return;
    	if (!newval) return;
    	if (newval > $scope.pics.length){
    		for (var i = 0; i <= newval - $scope.pics.length; i++){
    			(function(){
    				var newi = {
		    			picUrl: '',
		    			pageUrl: '',
		    			state: 0,
		    			type: $scope.type
		    		};

		    		newi.uploader = new FileUploader({
				        url: R.getFileUpload(),
				        formData: [{
				            type: 0
				        }],
				        autoUpload: true,
				        alias: 'inputFile'
				    });

				    newi.uploader.onCompleteItem = function(res, data){
				      	newi.picUrl = data.bizData.attachmentUrl;
				    };

	    			$scope.pics.push(newi);
    			})(i);
		    }
    	}
    	if (newval < $scope.pics.length){
    		$scope.pics = $scope.pics.splice(0, newval);
    	}
    });

    $scope.submit = function(){
        if ($scope.item.type){
        	var res = [];

        	angular.forEach($scope.pics, function(i){
        		res.push({
        			picUrl: i.picUrl,
        			pageUrl: i.pageUrl,
        			state: i.state,
        			type: i.type
        		});
        	});

            R.addBanner({'banners': res}).then(function(data){
                toaster.success("操作成功");
                $location.url("/app/banner/list");
            });
        }
    }
}]);